package com.shr.lease.common.exception;

import com.shr.lease.common.result.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理
 * @author Haoran Sun
 * @since 2024/06/18
 */

@ControllerAdvice
public class GlobalExceptionHandler {
    /**
     *
     * @param e 捕获的异常
     * @return 返回结果
     */

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result exceptionHandler(Exception e) {
        e.printStackTrace();
        return Result.fail();
    }

    /**
     * @param e 捕获Controller层的异常信息
     * @return 向前端相应异常信息用Result封装
     */
    @ExceptionHandler(value = LeaseException.class)
    @ResponseBody
    public Result leaseExceptionHandler(LeaseException e) {
        e.printStackTrace();
        return Result.fail(e.getCode(), e.getMessage());
    }

}
